package com.itheima.app.manager;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.TestSoul;
import com.itheima.domain.db.TestSoulLock;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.*;
import com.itheima.service.db.TestSoulLockService;
import com.itheima.service.db.TestSoulQuestionsOptionService;
import com.itheima.service.db.TestSoulService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.ReportService;
import com.itheima.service.mongo.ResultSelectService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.ErrorResult;
import com.itheima.vo.ReportVo;
import com.itheima.vo.TestSoulVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class TestManager {

    @DubboReference
    private TestSoulService testSoulService;
    @DubboReference
    private ReportService reportService;

    @DubboReference
    private UserInfoService userInfoService;
    @DubboReference
    private TestSoulLockService testSoulLockService;
    @DubboReference
    private ResultSelectService resultSelectService;

    @DubboReference
    private TestSoulQuestionsOptionService testSoulQuestionsOptionService;
    //测灵魂-问卷列表（学生实战）
    public ResponseEntity findTest() {
        //获取线程中的userId
        Long userId = UserHolder.get().getId();
        //调用rpc获取问卷列表
        List<TestSoul> soulList = testSoulService.findTest(userId);
        //声明voList
        List<TestSoulVo> voList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(soulList)) {
            for (TestSoul testSoul : soulList) {
                TestSoulVo vo = new TestSoulVo();
                vo.setId(testSoul.getId().toString());
                vo.setCover(testSoul.getCover());
                //查询解锁状态,也可以用户注册时增加解锁状态，保证不为空
                TestSoulLock testSoulLock = testSoulLockService.findByUserId(userId, testSoul.getId());
                if (testSoulLock==null) {
                    testSoulLock = new TestSoulLock();
                    testSoulLock.setUserId(userId);
                    testSoulLock.setTestSoulId(testSoul.getId());
                    testSoulLock.setLevel(testSoul.getLevel());
                    if (StrUtil.equals(testSoul.getLevel(),"初级")) {
                        testSoulLock.setIsLock(0);
                    }else {
                        testSoulLock.setIsLock(1);
                    }
                    testSoulLockService.save(testSoulLock);
                }
                vo.setIsLock(testSoulLock.getIsLock());
                //vo.setIsLock(testSoul.getIsLock());
                vo.setLevel(testSoul.getLevel());
                vo.setName(testSoul.getName());
                vo.setQuestions(testSoul.getQuestions());
                vo.setStar(testSoul.getStar());
                //查看是否有问卷报告记录
                String reportId = reportService.findNearReport(userId);
                vo.setReportId(reportId);
                voList.add(vo);
            }
        }
        return ResponseEntity.ok(voList);
    }
    //测灵魂-提交问卷（学生实战）
    public ResponseEntity sendTest(Map<String, String>[] answers) {
        //获取userId
        Long userId = UserHolder.get().getId();
        //查询得分
        TestScore testScore = testSoulQuestionsOptionService.sumScore(answers,userId);
        //查询类型
        Integer score=testScore.getSum();
        ResultSelect resultSelect = resultSelectService.findByScore(score);
        //封装对象
        Report report = new Report();
        report.setId(ObjectId.get());
        report.setScore(score);
        report.setConclusion(resultSelect.getConclusion());
        report.setCover(resultSelect.getCover());
        report.setCreated(new Date());
        report.setUserId(userId);
        report.setConclusionId(resultSelect.getConclusionId());
        report.setTestSoulId(testScore.getTestId());
        report.setDimensions(resultSelect.getDimensions());
        //加相似的人
        List<SimilarYou> similarYous = new ArrayList<>();
        List<Report> reportList = reportService.findSim(resultSelect.getConclusionId(),testScore.getTestId());
        System.out.println(reportList);
        if (CollectionUtils.isNotEmpty(reportList)) {
            for (Report report1 : reportList) {
                //排除自己
                if (userId==report1.getUserId()) {
                    continue;
                }
                UserInfo userInfo = userInfoService.findById(report1.getUserId());
                SimilarYou similarYou = new SimilarYou();
                similarYou.setAvatar(userInfo.getAvatar());
                similarYou.setId(userInfo.getId().intValue());
                similarYous.add(similarYou);
            }
        }
        report.setSimilarYou(similarYous);
        //保存
        String id = reportService.save(report);
        return ResponseEntity.ok(id);
    }
    //测灵魂-查看结果（学生实战）
    public ResponseEntity reportRes(String reportId) {
        //调用rpc
        Report report = reportService.findById(reportId);
        //封装vo
        ReportVo vo = new ReportVo();
        vo.setConclusion(report.getConclusion());
        vo.setCover(report.getCover());
        vo.setDimensions(report.getDimensions());
        vo.setSimilarYou(report.getSimilarYou());

        return ResponseEntity.ok(vo);
    }



}
